<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	$task = $_REQUEST["task"];
	$pet_id = $_REQUEST["pet_id"];

	$reason = $_REQUEST["reason"];

	$i_date = $_REQUEST["i_date"];

	$cheque_no = $_REQUEST["cheque_no"];
	$branch_name = $_REQUEST["branch_name"];
	$bank_name = $_REQUEST["bank_name"];

	$month = $_REQUEST["month"];
	$year = $_REQUEST["year"];

	$val=$_REQUEST["val"];
	
	$val1 = explode(",", $val);
	$val1 = implode("','",$val1);

                $panngan2 = $_REQUEST["panngan2"];
                $panngan3 = $_REQUEST["panngan3"];
                $type_debt = $_REQUEST["type_debt"];
    
	$db = new myDBS;
	$db->selectDB("account");

	switch ($task) {

		case "approvedCheque":
			$sql ="UPDATE tbl_debt SET cheque_status = 'อนุมัติ', cheque_date = '$i_date' WHERE cheque_no = '$cheque_no'";
                                            $db->runSQL($sql);

			break;
		case "disapprovalCheque":
			$sql ="UPDATE tbl_debt SET cheque_status = 'ไม่อนุมัติ' WHERE cheque_no = '$cheque_no'";
                                            $db->runSQL($sql);
			break;
		case "print":
			//$sql ="UPDATE tbl_debt SET cheque_no = '$cheque_no', branch_name = '$branch_name', bank_name = '$bank_name', print_cheque_date = '$i_date', print_cheque_status = 'พิมพ์แล้ว' WHERE id in('$val1') and lvl1_id = '" .$_SESSION["lvl1_id"]. "' and approved_status =  'อนุมัติ' and month_year = '$year-$month'";

                                            /*
			for($i=0; $i < count($val); $i++) {
				$str_in = $str_in. "'". $val[$i]. "',";
			}
			$str_in = $str_in . "''";
                                             * 
                                             */
			
			
                                                $sql = "SELECT * FROM tbl_init , tbl_salary
                                        WHERE  `card_id` = id
                                        AND card_id IN('$val1') 
                                        AND tbl_salary.lvl1_id = '" .$_SESSION["lvl1_id"]. "'
                                        AND approved_status =  'อนุมัติ'
                                        and tbl_init.month_year = '$year-$month'
                                        AND tbl_init.panngan2_id =  '$panngan2'
                                        AND tbl_init.panngan3_id =  '$panngan3'
                                           ";
			
			//echo $sql, $type_debt;

			
			//$db1->selectDB("account");
			$result = $db->runSQL($sql);
			
			$db1 = NEW myDBS;
			$db1->selectDB("account");

			
			while ( $rows = $db->fetch_object ( $result )){
				
				//echo $rows2->total_all;

				$sql = "INSERT INTO tbl_debt 
				(card_id, lvl1_id, panngan2_id, panngan3_id, month_year, $type_debt, cheque_no, branch_name, bank_name, print_cheque_date, print_cheque_status)
				VALUES('$rows->card_id', '".$_SESSION["lvl1_id"]."', '$panngan2', '$panngan3', '$year-$month', '" .$rows->{$type_debt}. "', '$cheque_no', '$branch_name', '$bank_name', '$i_date', 'พิมพ์แล้ว')
				";
				//echo $sql;

				$db1->runSQL($sql);


			}

			$sql = "UPDATE tbl_init SET status = concat(status,',$type_debt')
                                                where card_id IN('$val1') 
                                        AND lvl1_id = '" .$_SESSION["lvl1_id"]. "'
                                        and month_year = '$year-$month'
                                        AND tbl_init.panngan2_id =  '$panngan2'
                                        AND tbl_init.panngan3_id =  '$panngan3'
				";
			$db1->runSQL($sql);
			echo $sql;
   
                      
                         
			break;
		case "paid":
			$sql = "UPDATE tbl_debt SET paid_status = 'จ่ายแล้ว', paid_date = NOW() WHERE cheque_no = '$cheque_no'";
                                            $db->runSQL($sql);
			break;

	}
	//echo $sql;
	//$db->runSQL($sql);
?>
